package com.naizhuai.network.upload;

import com.naizhuai.util.FileUtils;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * @author：lichenxi
 * @date 2018/1/4 11
 * email：525603977@qq.com
 * Fighting
 * <p>
 * for example
 * Uri photoUri = ... // get it from a file chooser or a camera intent
 * Uri videoUri = ... // get it from a file chooser or a camera intent
 * <p>
 * MultipartBody.Part photobody =prepareFilePart(key,photoUri);
 * MultipartBody.Part videobody =prepareFilePart(key,videoUri);
 * <p>
 * Call<ResponseBody> call = service.upload(description, body);
 * rxjava同理
 */

public class FileUpload {
    //用户头像上传模块

     public static final  String userThumb="userThumb";
    //所有的业务图片上传
     public static final  String adp="adp";
     private Context mContext;


    public FileUpload(Context context) {
        mContext = context;
    }

    @NonNull
    private RequestBody createPartFromString(String descriptionString) {
        return RequestBody.create(
                okhttp3.MultipartBody.FORM, descriptionString);
    }

    @NonNull
    private MultipartBody.Part prepareFilePart(String partName, Uri fileUri) {
        File file = FileUtils.getFile(mContext, fileUri);
        // create RequestBody instance from file
        RequestBody requestFile =
                RequestBody.create(
                        MediaType.parse(mContext.getContentResolver().getType(fileUri)),
                        file
                );

        // MultipartBody.Part is used to send also the actual file name
        return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
    }

    @NonNull
    public static MultipartBody.Part getPart(@NonNull String path) {
        File uploadFile = new File(path);
        RequestBody requestFile =
                RequestBody.create(
                        MediaType.parse(FileUtils.getMimeType(uploadFile)),
                        uploadFile
                );
        // MultipartBody.Part is used to send also the actual file nameRx
        return MultipartBody.Part.createFormData("thumbFile"
                , uploadFile.getName()
                , requestFile);
    }

    @NonNull
    public static MultipartBody.Part getParts(@NonNull String path) {
        File uploadFile = new File(path);
        RequestBody requestFile =
                RequestBody.create(
                        MediaType.parse(FileUtils.getMimeType(uploadFile)),
                        uploadFile
                );
        // MultipartBody.Part is used to send also the actual file name
        return MultipartBody.Part.createFormData("thumbFile[]"
                , uploadFile.getName()
                , requestFile);
    }
}
